package trie;


public class Trie {
    private TrieNode root=new TrieNode('/');

    //插入一个字符串
    public void insert(char[] text){
        TrieNode p=root;
        for (int i = 0; i < text.length; i++) {
            int index = text[i]-'a';
            if(p.children[index]==null){
                p.children[index]=new TrieNode(text[i]);
            }
            p=p.children[index];
        }
        p.isEndingChar=true;
    }

    public boolean find(char[] pattern){
        TrieNode p=root;
        for (int i = 0; i < pattern.length; i++) {
            int index = pattern[i]-'a';
            if(p.children[index]==null){
                return false;
            }
            p=p.children[index];
        }
        if(p.isEndingChar==false){
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Trie trie = new Trie();
        trie.insert("tangxiong".toCharArray());
        boolean b = trie.find("tangxiong".toCharArray());
        System.out.println(trie);
    }
}
